package spring.spring_blog.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import spring.spring_blog.common.exceptions.ParseTokenFail;
import spring.spring_blog.pojo.response.response;

import java.sql.SQLIntegrityConstraintViolationException;


//对异常的处理
@RestControllerAdvice
@Slf4j
public class exceptionAdvice {


    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    public exceptionAdvice(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }

    @ExceptionHandler(NumberFormatException.class)
    public response handleNumberFormatException(NumberFormatException e) {
        log.error(e.getMessage() + "参数格式转换错误");
        return response.fail("数据的格式错误");
    }

    @ExceptionHandler(HandlerMethodValidationException.class)
    public response handleHandlerMethodValidationException(HandlerMethodValidationException e) {
        log.error("发生异常：{}",e.getMessage(),e);
        return response.fail("参数校验失败");
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public response handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("发生异常：{}", e.getMessage());
        return response.fail("参数不能为空");
    }

    @ExceptionHandler(ParseTokenFail.class)
    public response handleParseTokenFail(ParseTokenFail e) {
        log.info("令牌验证未通过：{}", e.getMessage());
        return response.fail("令牌验证未通过，尝试重新登陆");
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public response handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        log.error("sql请求错误：{}", e.getMessage());
        return response.fail("账号名字重复");
    }

    @ExceptionHandler
    public response handleException(Exception e) {

        log.error(e.getMessage());
        return response.fail("请求参数错误");
    }


}
